<?php

namespace App\Http\Requests\User\Personal;

use App\Exceptions\GeneralException;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Http\FormRequest;

class JoinCompany extends FormRequest
{
	/**
	 * Date: 27/04/2018
	 * @author George
	 * @return bool
	 * @throws GeneralException
	 */
    public function authorize()
    {
    	$user = Auth::user();
    	$records = $user->joins()->latest()->first();
    	if ($records && $records->status === 'padding') {
			throw new GeneralException('您的申请还在处理，无法继续提交', 422);
		}
    	if ($user->company_id) {
			throw new GeneralException('您已经加入公司，无法申请加入', 422);
		}
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
        	'company_id' => 'required',
        ];
    }

	/**
	 * 定义错误响应消息
	 *
	 * Date: 27/04/2018
	 * @author George
	 * @return array
	 */
	public function messages()
	{
		return [
			'company_id.required' => '请选择公司'
		];
    }
}
